package com.lc.websocket;

import java.io.IOException;

import javax.websocket.ClientEndpoint;
import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;

/**
 * @author 逝兮诚
 * @date 2017年5月25日 上午9:30:38
 * websorket client
 */
@ClientEndpoint
public class PushClient {
	
	private Session session;

	@OnOpen
	public void onOpen(Session session) {
		this.session = session;
		System.out.println("client \"" + session.getId() + "\" connection success!");
	}

	@OnClose
	public void onClose(Session session) {
		System.out.println("client \"" + session.getId() + "\" close!");
	}
	
	@OnMessage
	public void processMessageFromServer(String message, Session session) {
		System.out.println("Message came from the server ! " + message);
	}
	
	/**
	 * send msg to service
	 * @param msg
	 * @throws IOException
	 */
	public void sendMsg(String msg) throws IOException {
		session.getBasicRemote().sendText(msg);
	}
}
